home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 3986 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.2 KB

  1. Message-ID: <31098A65.1A40@peoplesoft.com>
  2. Date: Fri, 26 Jan 1996 18:13:57 -0800
  3. From: Randal Kuramoto <randal_kuramoto@peoplesoft.com>
  4. Organization: PeopleSoft
  5. X-Mailer: Mozilla 2.0b5 (WinNT; I)
  6. MIME-Version: 1.0
  7. Newsgroups: comp.lang.c++
  8. Subject: Limited generalization of templatized classes
  9. Content-Type: text/plain; charset=us-ascii
  10. Content-Transfer-Encoding: 7bit
  11. NNTP-Posting-Host: rkuramo2
  12. Path: ple-news-01.peoplesoft.com!
  13.  
  14. Hi,
  15.  
  16. I ran into (perhaps?) a subtle improvement on a piece of templatized code.
  17.  
  18. Let's say I have a mixin class such as:
  19.  
  20. class MixinX
  21. {
  22. public:
  23.    virtual ~MixinX() = 0; 
  24.  
  25.    Foo();
  26. };
  27.  
  28.  
  29. Now, I want to create a templatized class that uses derived classes from
  30. the Mixin class.  I have two versions:
  31.  
  32. Here is version 1:
  33.  
  34. template< typename T >
  35. class ClassY
  36. {
  37. public:
  38.     ClassY( T* pT ) : m_pT( pT ) {};                                     //!!!!!
  39.  
  40.     void Func() { m_pT->Foo(); };      // Calls MixinX::Foo()
  41.  
  42. private:
  43.     T* m_pT;
  44. };
  45.  
  46.  
  47. Here is version 2:
  48.  
  49. template< typename T >
  50. class ClassY
  51. {
  52. public:
  53.     ClassY( MixinX* pT ) : m_pT( pT ? dynamic_cast< T* >( pT ) : 0 ) {}; //!!!!!
  54.  
  55.     void Func() { m_pT->Foo(); };
  56.  
  57. private:
  58.     T* m_pT;
  59. };
  60.  
  61.  
  62. The difference between the versions is in the ctor parameter type.
  63.  
  64. In version 1, errors are caught if the linker cannot find a T::Foo().
  65. If a class Z just happens to have a function with the same signature
  66. as the MixinX::Foo()...it's possible if Foo is as simple as show here..., 
  67. then ClassY is using a class of objects that may not quite according
  68. the the behavior specified by the MixinX class.
  69.  
  70. In version 2, I don't like the downcast, but the compiler catches the 
  71. problem if typename T is not specifically a derived version of MixinX.
  72. The downcast is done only once in the ctor, and it also helps catch
  73. misnamed pointers at run-time.
  74.  
  75. I ran into this when I was looking at Scott Meyer's _More Effective C++_.
  76. The auto-pointer to a reference-counted object example in his book has
  77. a template that is similar to version 1 of my example.  With the version 2
  78. tweak, have I made the templatized class better?
  79.  
  80. Does anyone see any disadvantages or alternatives to version 2?
  81.  
  82. BTW, the _More Effective C++_ became available on Jan 24 from Addison-Wesley.
  83. It's a fantastic complement to _Effective C++_.
  84.  
  85. Randal
  86.